package base.server;


import base.adapter.EchoHandlerAdp;
import base.auth.LoginAuthRespHandler;
import base.heart.HeartBeatRespHandler;
import base.model.NettyMessageDecoder;
import base.model.NettyMessageEncoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;

/**
 * Created by wuhui on 2016/8/4.
 */
public class NettyServer {

    public static void main(String[] args) throws InterruptedException {
        new NettyServer().bind();
    }
    public void bind() throws InterruptedException {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG,100);
        b.handler(new LoggingHandler(LogLevel.DEBUG));
        b.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel channel) throws Exception {
                //一个包在环境中周期是按这里adap前后顺序走的

         //       channel.pipeline().addLast("loginAuth",new LoginAuthRespHandler());
                channel.pipeline().addLast(new EchoHandlerAdp());
             //   channel.pipeline().addLast(new NettyMessageDecoder(1024*1024,4,4,-8,0));
                channel.pipeline().addLast("MessageEncoder",new NettyMessageEncoder());
            //    channel.pipeline().addLast("readTimeout",new ReadTimeoutHandler(50));

            //     channel.pipeline().addLast("heartBeat",new HeartBeatRespHandler());
            }
        });
        b.bind("172.16.100.224",8080).sync();

    }

}
